<?php

namespace plugin\webman\foursport;
use support\Db;
use GatewayWorker\Lib\Gateway;
use support\Redis;
use plugin\webman\common\Common;


class Method3
{

  static function npcinfo(){
        $data=[
            ["id"=>"1","name"=>"城主"],
            ["id"=>"2","name"=>"工人"],
            ["id"=>"3","name"=>"刺客"],
            [ "id"=>"4","name"=>"管家"],
            // [ "id"=>"5","name"=>"船长猿"],
            // [ "id"=>"6","name"=>"牛仔猿"],
        ];
       return $data;
  }
  static function getnpcname($id){
      $data=self::npcinfo();
      foreach ($data as $key => $value) {
          if($value['id']==$id){
              return $value['name'];
          }
      }
  }

  static function getbetinfo($uid,$ecpect){
     $npcid = [1,2,3,4];
    //  用户投注每个的金额
     foreach ($npcid as $key => $value) {
      $a['npcid']=$value;
      $a['user_betnums']= (string)Db::table("wa_sports_touzhu")->where(['uid'=>$uid,'expect'=>$ecpect,'npcid'=>$value])->sum("coin");
      $bet[]=$a;
     }
    //  获取每个赛道的投注总额
    foreach ($npcid as $k => $v) {
      $b['npcid']=$v;
      $b['bet_total']= (string)Db::table("wa_sports_touzhu")->where(['expect'=>$ecpect,'npcid'=>$v])->sum("coin");
      $bet_total[]=$b;
    }
    return ['user_bet'=>$bet,'saidao_bet'=>$bet_total];
  }


  static function getsaidao($expect){
    $npcid = [1,2,3,4];
    foreach ($npcid as $k => $v) {
      $b['npcid']=$v;
      $b['bet_total']= (string)Db::table("wa_sports_touzhu")->where(['expect'=>$expect,'npcid'=>$v])->sum("coin");
      $bet_total[]=$b;
    }
    return ['saidao_bet'=>$bet_total];
  }

  static function getnpcid($name){
      $data=self::npcinfo();
      foreach ($data as $key => $value) {
          if($value['name']==$name){
              return $value['id'];
          }
      }
  }

//   获取运动员近100期冠军次数
  static function get_npc_win_num(){
      $expect = time()-60*100;
      $data=self::npcinfo();
      $win_num=array();
      $list = [];
      foreach ($data as $key => $value){
        $value['mingci'] = Db::table("wa_sports_paiming")->where("npcid",$value['id'])->where("mingci",1)->count();
       
        $list[]=$value;
      }
      
      return $list;
  }

  // 随机生成不重复的npc 1-6名次

  static function getmingci(){
      $expect = Common::getexpect();
      $data=self::npcinfo();
      $list = [];
      $mingci=array();
      foreach ($data as $key => $value){
        $mingci[]=$value['id'];
      }
      $mingci=array_unique($mingci);
      shuffle($mingci);
      $mingci=array_slice($mingci,0,4);
      foreach ($mingci as $key => $value){
        $a['id']=$value;
        $a['name']=self::getnpcname($value);
        $a['mingci']=$key+1;
        $list[]=$a;
      }
      // 入库记录名次
      foreach ($list as $key => $value){
        $record = [
          'npcid'=>$value['id'],
          'npcname'=>$value['name'],
          'mingci'=>$value['mingci'],
          'expect'=>$expect,
          'addtime'=>time()
        ];
        Db::table("wa_sports_paiming")->insert(
          $record 
        );
      }
      $arr=json_encode($list);
      Common::setRedis('mingci_'.$expect,$arr);
      return $list;
  }

  // 获取缓存中的mingci_expect

  static function getmingci_expect(){
      $expect = Common::getexpect();
      $arr2=Common::getRedis('mingci_'.$expect);
      if(!$arr2){
        $arr=self::npcinfo();
        foreach ($arr as $key => $value){
          $value['mingci']=0;
          $arr2[]=$value;
        }
      }else{
        $arr2 = json_decode($arr2,true);
      }
      
      return $arr2;
  }

  // 中奖后的更改结果 type: 1中奖  0未中奖
  static function zhongjiang($id,$guanjun,$type,$reward=''){
    if($type==1){
      $up = Db::table("wa_sports_touzhu")->where('id',$id)->update(['ifwin'=>1,'result'=>$guanjun['id'],'result_name'=>$guanjun['name'],'reward'=>$reward]);
      // 返还积分
     
    }else{
      $up = Db::table("wa_sports_touzhu")->where('id',$id)->update(['ifwin'=>0,'result'=>$guanjun['id'],'result_name'=>$guanjun['name']]);
    }
    
    if($up){
      return true;
    }else{
      return false;
    }
    
  }



}